package org.apache.hadoop.hdfs.server.namenode;

import java.io.Closeable;
import java.io.IOException;

import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
import org.apache.hadoop.hdfs.server.common.Storage;
import org.apache.hadoop.hdfs.server.common.Storage.FormatConfirmable;
import org.apache.hadoop.hdfs.server.common.StorageInfo;
import org.apache.hadoop.hdfs.server.protocol.NamespaceInfo;

/**
 * JournalManager类是负责在特定存储目录上持久化editlog文件的类，
 *   format()方法负责格式化底层存储，
 *   startLogSegment()方法负责从指定事务id开始记录一个操作的段落，
 *   finalizeLogSegment()方法负责完成指定事务id区间的写操作。
 *     这里之所以抽象这个接口， 是因为Namenode可能将editlog文件持久化到不同类型的存储上， 也就需要不同类型的JournalManager来管理， 所以需要定义一个抽象的接口。
 *
 * JoumalManager有多个子类，
 * - 普通的文件系统由FileJournalManager类管理、
 * - 共享NFS由BackupJournalManager类管理、
 * - Bookkeeper由BookkeeperJournalManager类管理、
 * - Quorum集群则由QuorumJournalManager类管理。
 */
@InterfaceAudience.Private
@InterfaceStability.Evolving
public interface JournalManager extends Closeable, LogsPurgeable, FormatConfirmable {

  /**
   * 格式化底层存储
   */
  void format(NamespaceInfo ns) throws IOException;

  /**
   * 从指定事务id开始记录一个操作的段落
   */
  EditLogOutputStream startLogSegment(long txId, int layoutVersion) throws IOException;

  /**
   * 完成指定事务id区间的写操作
   * 因为Namenode可能将editlog文件持久化到不同类型的存储上， 也就需要不同类型的JournalManager来管理， 所以需要定义一个抽象的接口。
   */
  void finalizeLogSegment(long firstTxId, long lastTxId) throws IOException;

  /**
   * Set the amount of memory that this stream should use to buffer edits
   */
  void setOutputBufferCapacity(int size);

  /**
   * Recover segments which have not been finalized.
   */
  void recoverUnfinalizedSegments() throws IOException;
  
  /**
   * Perform any steps that must succeed across all JournalManagers involved in
   * an upgrade before proceeding onto the actual upgrade stage. If a call to
   * any JM's doPreUpgrade method fails, then doUpgrade will not be called for
   * any JM.
   */
  void doPreUpgrade() throws IOException;
  
  /**
   * Perform the actual upgrade of the JM. After this is completed, the NN can
   * begin to use the new upgraded metadata. This metadata may later be either
   * finalized or rolled back to the previous state.
   * 
   * @param storage info about the new upgraded versions.
   */
  void doUpgrade(Storage storage) throws IOException;
  
  /**
   * Finalize the upgrade. JMs should purge any state that they had been keeping
   * around during the upgrade process. After this is completed, rollback is no
   * longer allowed.
   */
  void doFinalize() throws IOException;
  
  /**
   * Return true if this JM can roll back to the previous storage state, false
   * otherwise. The NN will refuse to run the rollback operation unless at least
   * one JM or fsimage storage directory can roll back.
   * 
   * @param storage the storage info for the current state
   * @param prevStorage the storage info for the previous (unupgraded) state
   * @param targetLayoutVersion the layout version we intend to roll back to
   * @return true if this JM can roll back, false otherwise.
   */
  boolean canRollBack(StorageInfo storage, StorageInfo prevStorage,
      int targetLayoutVersion) throws IOException;
  
  /**
   * Perform the rollback to the previous FS state. JMs which do not need to
   * roll back their state should just return without error.
   */
  void doRollback() throws IOException;
  
  /**
   * @return the CTime of the journal manager.
   */
  long getJournalCTime() throws IOException;

  /**
   * Discard the segments whose first txid is >= the given txid.
   * @param startTxId The given txid should be right at the segment boundary, 
   * i.e., it should be the first txid of some segment, if segment corresponding
   * to the txid exists.
   */
  void discardSegments(long startTxId) throws IOException;

  /**
   * Close the journal manager, freeing any resources it may hold.
   */
  @Override
  void close() throws IOException;
  
  /** 
   * Indicate that a journal is cannot be used to load a certain range of 
   * edits.
   * This exception occurs in the case of a gap in the transactions, or a
   * corrupt edit file.
   */
  public static class CorruptionException extends IOException {
    static final long serialVersionUID = -4687802717006172702L;
    
    public CorruptionException(String reason) {
      super(reason);
    }
  }

}
